package com.lucenten.collect.gnss.handler.tools;

import cn.quevo.common.lang.NumberUtils;
import cn.quevo.common.lang.StringUtils;
import com.lucenten.collect.gnss.entity.SatelliteStation;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

@Slf4j
public class StationInfoTools {

    public static boolean readStationInfoTxt(Path infoFile, SatelliteStation station) {
        if (Files.notExists(infoFile)) {
            return false;
        }
        try {
            List<String> lines = Files.readAllLines(infoFile);
            for (String line : lines) {
                line = line.toLowerCase().trim();
                if (line.contains("antenna latitude")) { // 经度
                    station.setLatitude(formatDoubleValue(line.split(":")));
                    continue;
                }
                if (line.contains("antenna longitude")) {// 纬度
                    station.setLongitude(formatDoubleValue(line.split(":")));
                    continue;
                }
                if (line.contains("antenna elevation")) {// 海拔
                    station.setAntennaElevation(formatDoubleValue(line.split(":")));
                    continue;
                }
                if (line.contains("antenna height")) {// 天线高度
                    station.setAntennaHeight(formatDoubleValue(line.split(":")));
                    continue;
                }
                if (line.contains("antenna type")) {// 天线型号
                    station.setAntennaType(formatValue(line.split(":")));
                    continue;
                }
                if (line.contains("receiver type")) {// 接收机型号
                    station.setReceiverType(formatValue(line.split(":")));
                    continue;
                }
                if (line.contains("sample interval")) {// 时间间隔
                    station.setSampleInterval(formatDoubleValue(line.split(":")).intValue());
                    continue;
                }
            }
            return true;
        } catch (IOException e) {
            log.error("O文件站点信息解析失败："+e.toString(),e);
        }
        return false;
    }

    /**
     * 格式化Value
     *
     * @param split
     * @return
     */
    private static String formatValue(String[] split) {
        if (split.length <= 1 || StringUtils.isBlank(split[1].trim())) {
            return "";
        }
        return split[1].trim();
    }

    /**
     * 格式化Value
     *
     * @param split
     * @return
     */
    private static Double formatDoubleValue(String[] split) {
        if (split.length <= 1 || StringUtils.isBlank(split[1].trim())) {
            return null;
        }
        return NumberUtils.toDouble(split[1].trim());
    }
}
